require('./filter');
var _ = require('lodash');
var Router = require('koa-router');
var service = require('./service');
var router = module.exports = new Router();

router.redirect('/', '/home');

// 关于管家
router.get('/about', function * (next) {
  this.body = this.template.render('templates/about.html', this.query);
});

// 管家协议
router.get('/protocol', function * (next) {
  this.body = this.template.render('templates/protocol.html');
});

// 网站首页
router.get('/home', function * (next) {
  this.body = this.render('templates/home.html');
});

// 绑定房屋 - 入口
router.get('/bind/house', loginRequired, function * (next) {
  this.body = this.render('templates/house.html', {
    room: this.session['lc:room'],
  });
});

// 绑定房屋 - 社区
router.get('/bind/project', loginRequired, function * (next) {
  this.body = this.render('templates/project.html', {
    list: this.session.projects
  });
});

// 绑定房屋 - 群组
router.get('/bind/groups', loginRequired, function * (next) {
  var project = _.find(this.session.projects, {
    id: this.query.projectId
  });
  if (project.groups.length === 0) {
    return this.redirect('/bind/building?projectId=' + project.id);
  }
  this.body = this.render('templates/groups.html', {
    project: project
  });
});

// 绑定房屋 - 楼栋
router.get('/bind/building', loginRequired, function * (next) {
  var data = yield service.listBuilding(this, this.query.projectId, this.query.groupId);
  var project = _.find(this.session.projects, {
    id: this.query.projectId
  });
  this.body = this.render('templates/building.html', data, {
    project: project,
    group: _.find(project.groups, {
      groupCode: this.query.groupId
    }),
  });
});

// 绑定房屋 - 房间
router.get('/bind/room', loginRequired, function * (next) {
  var data = yield service.listRoom(this, this.query.id);
  this.body = this.render('templates/room.html', data);
});

// 绑定房屋 - 选择
router.get('/bind/choose', loginRequired, function * (next) {
  var data = yield service.getRoom(this, this.query.id);
  this.session['lc:room'] = data && data.entity;
  this.redirect('/bind/house');
});

// 绑定房屋 - 生成二维码
router.get('/bind/qrcode', loginRequired, function * (next) {
  var room = this.session['lc:room'];
  if (!room) {
    return this.redirect('/bind/building');
  }
  // BOSS后台用户ID
  var data = yield service.quickmark(this, room.id, this.query.identity, this.session.puser.partnerId);
  this.body = this.render('templates/qrcode.html', data, {
    room: room,
    identity: this.query.identity,
  });
});

// 绑定记录
router.get('/bind/records', function * (next) {
  var data = yield service.listBinds(this, this.session.puser.partnerId);
  this.body = this.render('templates/records.html', data);
});

// 绑定记录
router.get('/bind/ajax/records', function * (next) {
  var self = this;
  yield service.listBinds(this, this.session.puser.partnerId, this.query.page, this.query.size).then(function (data) {
    self.body = self.render(200, data);
  }, function (data) {
    self.body = self.render(500, data);
  });
});